package com.ff.xyh.common.utils;

import com.alibaba.fastjson.JSON;
import com.ff.xyh.common.entity.result.ResultCode;
import com.ff.xyh.common.entity.result.ResultObject;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther FF
 * @Date 2022年 09月 21日
 * @Time 00:50
 */

public class CommonUtil {

    public static void main(String[] args) {
//        System.out.println(isPhoneNumber("1912071884")); // false 前三位满足 后面不满8位;
//        System.out.println(isPhoneNumber("19111111111")); // true 前三位满足 后面满8位;
//        long before = System.currentTimeMillis();
//        new MyJDKUtil().getSum2(1, 1000000000l);
//        System.out.println(System.currentTimeMillis()-before);
//        System.out.println(System.currentTimeMillis());
//        CopyOnWriteArraySet<String> roomUsers = new CopyOnWriteArraySet<>();
//        System.out.println(roomUsers.add("123"));
//        System.out.println(roomUsers.add("123"));
//        Map<String, String> map = new HashMap<>();


    }
    public static String getFiledValue(Object obj, String filedName){
        if (obj == null) {
            return null;
        } else {
            String value = null;
            try {
                Field field = obj.getClass().getDeclaredField(filedName);
                // 绕过安全检查
                field.setAccessible(true);
                value = (String) field.get(obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return value;
        }
    }

    public static boolean isPhoneNumber(String phoneNumber) {
        // 前三位满足这些号码段 后八位0-9
        String regExp = "^(" +
                "(13[0-9])" +
                "|(14[5,7,9])" +
                "|(15([0-3]|[5-9]))" +
                "|(16[5,6])" +
                "|(17[0-8])" +
                "|(18[0-9])" +
                "|(19[1,5,8,9])" +
//                ")\\d{8}$";
                ")[0-9]{8}$";
        
        Pattern p = Pattern.compile(regExp);

        Matcher m = p.matcher(phoneNumber);

        return m.find();//boolean
    }



    public static int getConsumeLevel(int consumePrice) {
        if (consumePrice > 999)
            return 4;
        else if (consumePrice > 499)
            return 3;
        else if (consumePrice > 199)
            return 2;
        else if (consumePrice > 99)
            return 1;
        else
            return 0;
    }

//    public static Object getDataInResultObject(ResultObject resultObject) {
//        Map<String, Object> data = resultObject.getData();
//        if (data != null)
//            return data.get("data");
//        else
//            return null;
//    }
    public static <T> T getDataInResultObject(ResultObject resultObject, Class<T> classOfT) {
        if (resultObject.getCode() == ResultCode.ERROR_CODE)
            return null;
        Map<String, Object> data = resultObject.getData();
        if (data != null) {
            String jsonString = JSON.toJSONString(data.get("data"));
            return JSON.parseObject(jsonString, classOfT);
        }
        else
            return null;
    }

//    public static Object getItemsInResultObject(ResultObject resultObject) {
//        if (resultObject.getCode() == ResultCode.ERROR_CODE)
//            return null;
//        Map<String, Object> data = resultObject.getData();
//        if (data != null)
//            return data.get("items");
//        else
//            return null;
//    }

    public static <T> List<T> getItemsInResultObject(ResultObject resultObject, Class<T> tClass) {
        if (resultObject.getCode() == ResultCode.ERROR_CODE)
            return null;
        Map<String, Object> data = resultObject.getData();
        if (data != null) {
            List<T> items = (List<T>) data.get("items");//默认是map类型
            if (items.isEmpty()) return null;
            String s = JSON.toJSONString(items);
            items = JSON.parseArray(s, tClass);
            return items;
        } else {
            return null;
        }
    }

    public static <T> T getMessageInMap(Map map, Class<T> tClass) {
        String jsonString = JSON.toJSONString(map.get("message"));
        return JSON.parseObject(jsonString, tClass);
    }


    public int getSum(int i, int n) {
//        System.out.println(i);
        if (i < n)
            return i + getSum(++i, n);
        else
            return i ;
    }
    public void getSum2(long i, long n) {
        long sum = 0;
        while(i <= n)
            sum += i++;
        System.out.println(sum);
    }
    public void getSum3(long i, long n) {
        long sum = 0;
        for(;i <= n;i++)
            sum += i;
        System.out.println(sum);
    }

    public static boolean isEmptyList(List list) {
        return (list == null || list.isEmpty());
    }


    /**
     * 判断文件类型是否是Image
     * @param fileName
     * @param allowGif 允许gif
     * @return
     */
    public static boolean regFileNameIsImage(String fileName, boolean allowGif) {
        String name = fileName.toLowerCase();
        if (name.endsWith("jpg")
                || name.endsWith("jpeg")
                || name.endsWith("bmp")
                || name.endsWith("png")
                || (name.endsWith("gif") && allowGif)) {
            return true;
        }
        return false;
    }
    public static boolean regFileNameIsImage(String fileName) {
        return regFileNameIsImage(fileName, false);
    }
}
